<?php

/**
 * @Author:      zk
 * @DateTime:    18-7-12 上午11:27
 * @Description: OA 系统的管理账户
 */
class OAUser extends Eloquent
{
    protected $table = 'oa_user';

    /**
     * 用户状态数组
     * @var array 
     */
    public static $statusArr = [
        1 => '启用',
        2 => '停用',
        3 => '删除',
    ];
    const STATUS_START = 1;
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-12 下午4:22
     * @Description: 根据用户的用户名、密码获取用户信息
     * @param string $user_name 用户名
     * @param string $password  密码
     * @return array
     */
    public static function findUserByUP($user_name, $password){
        return OAUser::where('user_name', $user_name)->where('password', $password)->first();
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-12 下午4:22
     * @Description: 根据用户id获取用户信息 [OA 平台用户信息]
     * @param int $id 用户id
     * @return array
     */
    public static function getUserById($id){
        return OAUser::find($id);
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-20 上午11:08
     * @Description: 根据用户id获取用户信息 [ OA平台、HRM平台用户信息 ]
     * @param int $user_id 用户id
     * @return array
     */
    public static function getUserInfo($user_id){
        $user_info = self::getUserById($user_id);
        $result = [
            'id' => $user_id,
            'user_name' => $user_info->user_name,
            'real_name' => $user_info->real_name,
            'hrm_user_id' => $user_info->hrm_user_id,
        ];
        $hrm_user_info = HrmUser::getUserInfoById($user_info->hrm_user_id);     //获取hrm平台用户信息
        if(!$hrm_user_info){
            $result['hr_sex'] = 0;
            $result['hr_email'] = '';
            $result['hr_position_name'] = '';
            $result['hr_department_name'] = '';
            $result['hr_department_id'] = '';
        } else {
            if(empty($user_info->real_name)){
                $result['real_name'] = $hrm_user_info->hr_user_name;
            }
            $result['hr_sex'] = $hrm_user_info->hr_sex;
            $result['hr_email'] = $hrm_user_info->hr_email;
            $result['hr_position_name'] = $hrm_user_info->hr_position_name;
            $result['hr_department_name'] = $hrm_user_info->hr_department_name;
            $result['hr_department_id'] = $hrm_user_info->department_id;
        }
        $result['role'] = self::getUserAllRoleByUserId($user_id);

        return $result;
    }

    /**
     * @Author:      yyc
     * @DateTime:    18-7-25 下午15:58
     * @Description: 根据用户id获取用户角色信息
     * @param $userId int 用户ID
     * @return mixed
     */
    public static function getUserAllRoleByUserId($userId)
    {
        $result = OARole::select(['oa_role.id', 'role_name'])
            ->leftJoin('oa_user_role', 'oa_user_role.role_id', '=', 'oa_role.id')
            ->leftJoin('oa_user', 'oa_user.id', '=', 'oa_user_role.user_id')
            ->where('oa_user.id', '=', $userId)
            ->where('oa_role.status', self::STATUS_START)
            ->where('oa_user_role.status', self::STATUS_START)
            ->get()
            ->toArray();
        return $result;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-14 下午4:20
     * @Description: 根据hrm平台用户id获取用户在OA平台的信息 【 启用和未启用的用户 】
     * @param array $hrm_user_id_arr hrm平台用户id数组
     * @return array 以hrm_user_id为下标的二维数组
     */
    public static function getUserByHrmUserid($hrm_user_id_arr){
        $result = [];
        if(!is_array($hrm_user_id_arr)){
            return $result;
        }
        $select_arr = [
            'id', 'user_name', 'real_name', 'password', 'hrm_user_id', 'status',
        ];
        $list = OAUser::whereIn('hrm_user_id', $hrm_user_id_arr)
                ->whereIn('status', [1, 2])
                ->select($select_arr)
                ->get()
                ->toArray();
        if(empty($list)){
            return $result;
        }
        $user_id_arr = [];  //OA用户id数组
        foreach ($list as $k => $v) {
            $user_id_arr[] = $v['id'];
        }
        $role_info = OAUserRole::getRoleInfoByUserIdArr($user_id_arr);
        
        foreach ($list as $v) {
            $temp = $v;
            $temp['role_info'] = isset($role_info[$v['id']]) ? $role_info[$v['id']] : [];
            $result[$v['hrm_user_id']] = $temp;
        }
        return $result;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-19 下午4:40
     * @Description: 根据hrm平台用户id获取用户在OA平台的用户id数组   【 OA启用用户 】
     * @param array $hrm_user_id_arr hrm平台用户id数组
     * @return array OA用户id数组
     */
    public static function getUseridByHrmUserid($hrm_user_id_arr){
        $result = [];
        if(!is_array($hrm_user_id_arr)){
            return $result;
        }
        $select_arr = [
            'id', 'user_name', 'real_name', 'hrm_user_id', 'status',
        ];
        $list = OAUser::whereIn('hrm_user_id', $hrm_user_id_arr)
                ->where('status', 1)
                ->select($select_arr)
                ->get()
                ->toArray();
        
        if(!empty($list)){
            foreach ($list as $v) {
                $result[] = $v['id'];
            }
        }
        return $result;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-19 下午3:25
     * @Description: 获取OA平台下的所有有效的用户id
     * @param int $type     数据类型 [1:OA用户id数组;2:OA用户的hrm_user_id数组;  默认值为 1]
     * @return array 用户id组成的数组
     */
    public static function getAllUserid($type = 1) {
        $result = [];
        $select_arr = [
            'id', 'user_name', 'real_name', 'password', 'hrm_user_id', 'status',
        ];
        $list = OAUser::whereIn('status', [1, 2])
                ->select($select_arr)
                ->get()
                ->toArray();
        if(!empty($list)){
            if($type == 1){
                foreach ($list as $k => $v) {
                    $result[] = $v['id'];
                }
            }elseif ($type == 2) {
                foreach ($list as $k => $v) {
                    $result[] = $v['hrm_user_id'];
                }
            }
        }
        
        return $result;
    }
  
}
